package com.ruoyi.pms.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.SecurityUtils;
import org.apache.xmlbeans.GDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.pms.domain.PmsRequest;
import com.ruoyi.pms.service.IPmsRequestService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 采购申请Controller
 * 
 * @author ruoyi
 * @date 2021-11-04
 */
@RestController
@RequestMapping("/request")
public class PmsRequestController extends BaseController
{
    @Autowired
    private IPmsRequestService pmsRequestService;

    /**
     * 查询采购申请列表
     */
    @PreAuthorize(hasPermi = "pms:request:list")
    @GetMapping("/list")
    public TableDataInfo list(PmsRequest pmsRequest)
    {
        startPage();
        List<PmsRequest> list = pmsRequestService.selectPmsRequestList(pmsRequest);
        return getDataTable(list);
    }

    /**
     * 导出采购申请列表
     */
    @PreAuthorize(hasPermi = "pms:request:export")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsRequest pmsRequest) throws IOException
    {
        List<PmsRequest> list = pmsRequestService.selectPmsRequestList(pmsRequest);
        ExcelUtil<PmsRequest> util = new ExcelUtil<PmsRequest>(PmsRequest.class);
        util.exportExcel(response, list, "采购申请数据");
    }

    /**
     * 获取采购申请详细信息
     */
    @PreAuthorize(hasPermi = "pms:request:query")
    @GetMapping(value = "/{requestUuid}")
    public AjaxResult getInfo(@PathVariable("requestUuid") String requestUuid)
    {
        return AjaxResult.success(pmsRequestService.selectPmsRequestByRequestUuid(requestUuid));
    }

    /**
     * 新增采购申请
     */
    @PreAuthorize(hasPermi = "pms:request:add")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsRequest pmsRequest)
    {
        return toAjax(pmsRequestService.insertPmsRequest(pmsRequest));
    }

    /**
     * 修改采购申请
     */
    @PreAuthorize(hasPermi = "pms:request:edit")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update")
    public AjaxResult edit(@RequestBody PmsRequest pmsRequest)
    {
        return toAjax(pmsRequestService.updatePmsRequest(pmsRequest));
    }

    /**
     * 修改采购申请
     */
    @PreAuthorize(hasPermi = "pms:request:edit")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/confirm")
    public AjaxResult confirm(@RequestBody PmsRequest pmsRequest)
    {
        Date date = new Date();
        SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calender = Calendar.getInstance();
        int inow = calender.get(Calendar.MINUTE);
        calender.set(Calendar.MINUTE,inow-5);
        sdf.format(calender.getTime());
        String a = sdf.format(calender.getTime());

        return toAjax(pmsRequestService.confirmPmsRequest(pmsRequest));
    }
    /**
     * 删除采购申请
     */
    @PreAuthorize(hasPermi = "pms:request:remove")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{requestUuids}")
    public AjaxResult remove(@PathVariable String[] requestUuids)
    {
        return toAjax(pmsRequestService.deletePmsRequestByRequestUuids(requestUuids));
    }
}
